﻿using Microsoft.Win32;
using Newtonsoft.Json;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Windows;
using VisionDesigner;
using VisionDesigner.HPFeaturePatMatch;
using VM.Common.Enums;
using VM.Common.EventMgrLib;
using VM.Common.Helper;
using VM.Dialogs.Views;
using VM.Start.Attributes;
using VM.Start.Common;
using VM.Start.Common.Provide;
using VM.Start.Controls;
using VM.Start.Core;
using VM.Start.Events;
using VM.Start.Models;
using VM.Start.Services;
using VM.Start.ViewModels;

namespace Plugin.Matching
{
    #region enum
    [Serializable]
    public enum eLinkCommand
    {
        InputImageLink,
        MathNum,
        PositionFix
    }

    [Serializable]
    public enum eOperateCommand
    {
        创建,
        编辑,
        载入,
        导出
    }

    [Serializable]
    public class TemplateModel : NotifyPropertyBase
    {
        // 模板的序号
        private int _index;
        public int Index
        {
            get => _index;
            set => Set(ref _index, value);
        }

        // 模板的名称
        private string _name;
        public string Name
        {
            get => _name;
            set => Set(ref _name, value);
        }

        public EditViewModel Data { get; set; } // 模板数据
    }

    #endregion
    [Category("检测识别")]
    [DisplayName("模板匹配")]
    [ModuleImageName("Matching")]
    [JsonObject(MemberSerialization.OptOut)]
    public class MatchingViewModel : ModuleBase
    {
        #region  属性
        /// <summary>
        /// 匹配个数
        /// </summary>
        private LinkVarModel _MathNum = new LinkVarModel() { Value = 1 };
        public LinkVarModel MathNum
        {
            get { return _MathNum; }
            set { Set(ref _MathNum, value); }
        }
        [Newtonsoft.Json.JsonIgnore]
        private CHPFeaturePatMatchTool _HPFeaturePatMatchTool;  // 高精度匹配算子
        private CHPFeaturePatMatchTool HPFeaturePatMatchTool
        {
            get
            {
                try
                {
                    if (_HPFeaturePatMatchTool == null)
                    {
                        _HPFeaturePatMatchTool = new CHPFeaturePatMatchTool();
                    }
                }
                catch (MvdException ex)
                {
                    if (ex.ErrorCode == MVD_ERROR_CODE.MVD_E_RUNTIME)
                    {
                        MessageView.Ins.MessageBoxShow("运行环境异常，请停止项目并检查加密狗！", eMsgType.Error);
                        Logger.AddLog("运行环境异常，请停止项目并检查加密狗!", eMsgType.Error);
                    }
                    else
                    {
                        MessageView.Ins.MessageBoxShow("创建实例失败！", eMsgType.Error);
                        Logger.AddLog("创建实例失败！", eMsgType.Error);
                    }
                }
                return _HPFeaturePatMatchTool;
            }
        }

        public CHPFeaturePattern _HPFeaturePattern = null;             // 高精度模板算子

        /// <summary>
        /// 编辑模版Model
        /// </summary>
        private EditViewModel _editViewModel;
        public EditViewModel editViewModel
        {
            get
            {
                if (_editViewModel == null)
                {
                    _editViewModel = new EditViewModel();
                }
                return _editViewModel;
            }
            set { Set(ref _editViewModel, value); }
        }

        /// <summary>
        /// 参数
        /// </summary>
        private ParamModel _param = new ParamModel();
        public ParamModel Param
        {
            get => _param;
            set
            {
                Set(ref _param, value);
            }
        }
        #endregion
        #region  数据接口


        // 在 ViewModel 中
        private int _selectedTabIndex;
        public int SelectedTabIndex
        {
            get => _selectedTabIndex;
            set
            {
                _selectedTabIndex = value;
                RaisePropertyChanged(); // 通知属性更新
            }
        }

        [Newtonsoft.Json.JsonIgnore]
        private VMDisplayControl _MatchWindow;
        [Newtonsoft.Json.JsonIgnore]
        public VMDisplayControl MatchWindow
        {
            get
            {
                if (_MatchWindow == null)
                {
                    _MatchWindow = new VMDisplayControl();
                }
                return _MatchWindow;
            }
        }

        private ObservableCollection<TemplateModel> _templateList = new ObservableCollection<TemplateModel>();
        public ObservableCollection<TemplateModel> TemplateList
        {
            get { return _templateList; }
            set { _templateList = value; }
        }

        public TemplateModel _selectedTemplate;
        public TemplateModel SelectedTemplate
        {
            get
            {
                return _selectedTemplate;
            }
            set
            {
                Set(ref _selectedTemplate, value);
                if (_selectedTemplate != null)
                {
                    editViewModel = _selectedTemplate.Data;
                    MatchWindow.Copy(editViewModel.TemplateWindow);
                    _HPFeaturePattern = editViewModel.HPFeaturePattern;
                }
                else
                {
                    editViewModel = null;
                    MatchWindow.Clear();
                    MatchWindow.Image = null;
                    _HPFeaturePattern = null;
                }
            }
        }

        [Newtonsoft.Json.JsonIgnore]
        private ObservableCollection<ResultTemplateMatch> _ResultTemplateMatchs;
        [Newtonsoft.Json.JsonIgnore]
        public ObservableCollection<ResultTemplateMatch> ResultTemplateMatchs
        {
            get
            {
                if (_ResultTemplateMatchs == null)
                {
                    _ResultTemplateMatchs = new ObservableCollection<ResultTemplateMatch>();
                }
                return _ResultTemplateMatchs;
            }
            set { _ResultTemplateMatchs = value; }
        }

        /// <summary>
        /// ROI
        /// </summary>
        public CMvdShape ROI;
        private ROITypeEnum _ROIType = ROITypeEnum.全图;
        public ROITypeEnum ROIType
        {
            get { return _ROIType; }
            set
            {
                Set(ref _ROIType, value, new Action(() =>
                {
                    mWindowH.ROI = ROI;
                    switch (_ROIType)
                    {
                        case ROITypeEnum.全图:
                            mWindowH.ROIType = ROITypeEnum.全图;
                            break;
                        case ROITypeEnum.矩形:
                            mWindowH.ROIType = ROITypeEnum.矩形;
                            break;
                        case ROITypeEnum.圆:
                            mWindowH.ROIType = ROITypeEnum.圆;
                            break;
                        default:
                            break;
                    }
                }));
            }
        }

        #endregion
        #region  指令

        [Newtonsoft.Json.JsonIgnore]
        private CommandBase _LinkCommand;
        [Newtonsoft.Json.JsonIgnore]
        public CommandBase LinkCommand
        {
            get
            {
                if (_LinkCommand == null)
                {
                    //以GUID+类名作为筛选器
                    EventMgr.Ins
                        .GetEvent<VarChangedEvent>()
                        .Subscribe(OnVarChanged, o => o.SendName.StartsWith($"{ModuleGuid}"));
                    _LinkCommand = new CommandBase(
                        (obj) =>
                        {
                            eLinkCommand linkCommand = (eLinkCommand)obj;
                            switch (linkCommand)
                            {
                                case eLinkCommand.InputImageLink:
                                    CommonMethods.GetModuleList(ModuleParam, VarLinkViewModel.Ins.Modules, "Image");
                                    EventMgr.Ins.GetEvent<OpenVarLinkViewEvent>().Publish($"{ModuleGuid},InputImageLink");
                                    break;
                                case eLinkCommand.PositionFix:
                                    CommonMethods.GetModuleList(ModuleParam, VarLinkViewModel.Ins.Modules, "CPositionFixTool");
                                    EventMgr.Ins.GetEvent<OpenVarLinkViewEvent>().Publish($"{ModuleGuid},PositionFix");
                                    break;
                                case eLinkCommand.MathNum:
                                    CommonMethods.GetModuleList(ModuleParam, VarLinkViewModel.Ins.Modules, "int");
                                    EventMgr.Ins.GetEvent<OpenVarLinkViewEvent>().Publish($"{ModuleGuid},MathNumLink");
                                    break;
                                default:
                                    break;
                            }

                        }
                    );
                }
                return _LinkCommand;
            }
        }

        [Newtonsoft.Json.JsonIgnore]
        private CommandBase _OperateCommand;
        [Newtonsoft.Json.JsonIgnore]
        public CommandBase OperateCommand
        {
            get
            {
                if (_OperateCommand == null)
                {
                    _OperateCommand = new CommandBase(
                        (obj) =>
                        {
                            try
                            {
                                eOperateCommand par = (eOperateCommand)obj;
                                switch (par)
                                {
                                    case eOperateCommand.创建:
                                        try
                                        {
                                            editViewModel = new EditViewModel();
                                            EditView editView = new EditView
                                            {
                                                DataContext = editViewModel
                                            };
                                            editViewModel.view = editView;
                                            editViewModel.LocalImage = DispImage;
                                            editViewModel.matchingViewModel = this;
                                            _HPFeaturePattern = new CHPFeaturePattern();
                                            editViewModel.HPFeaturePattern = this._HPFeaturePattern;
                                            var view = this.ModuleView as MatchingView;
                                            editView.Owner = view;
                                            editViewModel.Loaded();
                                            editView.ShowDialog();
                                        }
                                        catch (MvdException ex)
                                        {
                                            if (ex.ErrorCode == MVD_ERROR_CODE.MVD_E_RUNTIME)
                                            {
                                                MessageView.Ins.MessageBoxShow("运行环境异常，请停止项目并检查加密狗！", eMsgType.Error);
                                                Logger.AddLog("运行环境异常，请停止项目并检查加密狗!", eMsgType.Error);
                                            }
                                            else
                                            {
                                                MessageView.Ins.MessageBoxShow("创建实例失败！", eMsgType.Error);
                                                Logger.AddLog("创建实例失败！", eMsgType.Error);
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            // 记录日志或显示错误消息
                                            MessageView.Ins.MessageBoxShow($"创建操作失败: {ex.Message}", eMsgType.Error);
                                            Logger.AddLog($"创建操作失败: {ex.Message}", eMsgType.Error);
                                        }
                                        break;
                                    case eOperateCommand.编辑:
                                        try
                                        {
                                            if (SelectedTemplate != null && SelectedTemplate.Data != null)
                                            {
                                                EditView editView = new EditView
                                                {
                                                    DataContext = SelectedTemplate.Data
                                                };
                                                SelectedTemplate.Data.LocalImage = DispImage;
                                                SelectedTemplate.Data.view = editView;
                                                SelectedTemplate.Data.matchingViewModel = this;
                                                SelectedTemplate.Data.m_TemplateModel = SelectedTemplate;
                                                var view = this.ModuleView as MatchingView;
                                                editView.Owner = view;
                                                editViewModel.Loaded();
                                                editView.ShowDialog();
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            Logger.GetExceptionMsg(ex);
                                        }
                                        break;
                                    case eOperateCommand.载入:
                                        try
                                        {
                                            OpenFileDialog fileDlg = new OpenFileDialog
                                            {
                                                Filter = "XML Files|*.hpmxml;*.xml",
                                                FilterIndex = 2,
                                                RestoreDirectory = true
                                            };
                                            if (fileDlg.ShowDialog() == true) // WPF的对话框返回bool
                                            {
                                                string filePath = fileDlg.FileName;
                                                string Name = Path.GetFileNameWithoutExtension(filePath);
                                                if (File.Exists(filePath))
                                                {
                                                    CHPFeaturePattern cHPFeaturePattern = new CHPFeaturePattern();
                                                    cHPFeaturePattern.ImportPattern(filePath);
                                                    EditViewModel editVModel = new EditViewModel();
                                                    editViewModel = editVModel;
                                                    editViewModel.HPFeaturePattern = cHPFeaturePattern;
                                                    _HPFeaturePattern = cHPFeaturePattern;
                                                    TemplateModel templateData = new TemplateModel();
                                                    templateData.Name = Name;
                                                    templateData.Index = TemplateList.Count;
                                                    templateData.Data = editViewModel;
                                                    TemplateList.Add(templateData);
                                                    SelectedTemplate = templateData;
                                                }
                                            }
                                        }
                                        catch (MvdException ex)
                                        {
                                            // 处理自定义异常
                                            MessageView.Ins.MessageBoxShow($"导入XML文件失败. 错误码: 0x{ex.ErrorCode:X}, 信息: {ex.Message}", eMsgType.Error);
                                            Logger.AddLog($"导入XML文件失败. 错误码: 0x{ex.ErrorCode:X}, 信息: {ex.Message}", eMsgType.Error);
                                        }
                                        catch (Exception ex)
                                        {
                                            // 处理通用异
                                            MessageView.Ins.MessageBoxShow($"导入XML文件失败: {ex.Message}", eMsgType.Error);
                                            Logger.AddLog($"导入XML文件失败: {ex.Message}", eMsgType.Error);
                                        }
                                        break;
                                    case eOperateCommand.导出:
                                        if (_HPFeaturePattern == null || _HPFeaturePattern.ModelData == null)
                                        {
                                            MessageView.Ins.MessageBoxShow("当前模板没有数据！导出失败！", eMsgType.Error);
                                            Logger.AddLog("当前模板没有数据！导出失败！", eMsgType.Error);
                                            return;
                                        }
                                        else if (_HPFeaturePattern.ModelData != null && _HPFeaturePattern.Result == null)
                                        {
                                            if (_HPFeaturePattern.InputImage == null || _HPFeaturePattern.InputImage.IsEmpty)
                                            {
                                                if (SelectedTemplate.Data.TrainImage == null && SelectedTemplate.Data.TrainImage.IsEmpty)
                                                {
                                                    MessageView.Ins.MessageBoxShow("当前模板没有训练图像！导出失败！", eMsgType.Error);
                                                    Logger.AddLog("当前模板没有训练图像！导出失败！", eMsgType.Error);
                                                    return;
                                                }
                                                _HPFeaturePattern.InputImage = SelectedTemplate.Data.TrainImage;
                                            }

                                            _HPFeaturePattern.Train();
                                        }
                                        try
                                        {
                                            SaveFileDialog fileDlg = new SaveFileDialog
                                            {
                                                Filter = "XML Files|*.hpmxml",
                                                RestoreDirectory = true,
                                                FileName = SelectedTemplate?.Name ?? "NewTemplate" // 防止 SelectedTemplate 为 null
                                            };
                                            if (fileDlg.ShowDialog() == true) // WPF的对话框返回bool
                                            {
                                                string filePath = fileDlg.FileName;
                                                _HPFeaturePattern.ExportPattern(filePath);
                                            }
                                        }
                                        catch (MvdException ex)
                                        {
                                            // 处理自定义异常
                                            MessageView.Ins.MessageBoxShow($"导出模型文件失败. 错误码: 0x{ex.ErrorCode:X}, 信息: {ex.Message}", eMsgType.Error);
                                            Logger.AddLog($"导出模型文件失败. 错误码: 0x{ex.ErrorCode:X}, 信息: {ex.Message}", eMsgType.Error);
                                        }
                                        catch (Exception ex)
                                        {
                                            // 处理通用异常
                                            MessageView.Ins.MessageBoxShow($"导出模型文件失败: {ex.Message}", eMsgType.Error);
                                            Logger.AddLog($"导出模型文件失败: {ex.Message}", eMsgType.Error);
                                        }
                                        break;
                                    default:
                                        throw new ArgumentOutOfRangeException();
                                }
                            }
                            catch (Exception ex)
                            {
                                Logger.GetExceptionMsg(ex);
                            }
                        }
                    );
                }
                return _OperateCommand;
            }
        }

        [Newtonsoft.Json.JsonIgnore]
        private CommandBase _ExecuteCommand;
        [Newtonsoft.Json.JsonIgnore]
        public CommandBase ExecuteCommand
        {
            get
            {
                if (_ExecuteCommand == null)
                {
                    _ExecuteCommand = new CommandBase(
                        (obj) =>
                        {
                            ExeModule();
                        }
                    );
                }
                return _ExecuteCommand;
            }
        }

        [Newtonsoft.Json.JsonIgnore]
        private CommandBase _ConfirmCommand;
        [Newtonsoft.Json.JsonIgnore]
        public CommandBase ConfirmCommand
        {
            get
            {
                if (_ConfirmCommand == null)
                {
                    _ConfirmCommand = new CommandBase(
                        (obj) =>
                        {
                            var view = this.ModuleView as MatchingView;
                            if (view != null)
                            {
                                view.Close();
                            }
                        }
                    );
                }
                return _ConfirmCommand;
            }
        }

        #endregion
        #region  方法
        public override void Loaded()
        {
            base.Loaded();
            //ClosedView = true;
            foreach (var item in TemplateList)
            {
                item.Data.Loaded();
            }
            var matchingItem = TemplateList.FirstOrDefault(item => item.Name == SelectedTemplate.Name && item.Index == SelectedTemplate.Index);
            SelectedTemplate = matchingItem;
        }

        public override void SetDefaultLink()
        {
            if (InputImageLinkText == null)
            {
                CommonMethods.GetModuleList(ModuleParam, VarLinkViewModel.Ins.Modules, "Image");
                var moduls = VarLinkViewModel.Ins.Modules.LastOrDefault();
                if (moduls?.VarModels.Count > 0)
                {
                    InputImageLinkText = $"&{moduls.DisplayName}.{moduls.VarModels[0].Name}";
                }
            }

            if (PositionFixText == null)
            {
                CommonMethods.GetModuleList(ModuleParam, VarLinkViewModel.Ins.Modules, "CPositionFixTool");
                var moduls = VarLinkViewModel.Ins.Modules.LastOrDefault();
                if (moduls?.VarModels.Count > 0)
                {
                    PositionFixText = $"&{moduls.DisplayName}.{moduls.VarModels[0].Name}";
                }
            }
        }

        private void OnVarChanged(VarChangedEventParamModel obj)
        {
            switch (obj.SendName.Split(',')[1])
            {
                case "InputImageLink":
                    InputImageLinkText = obj.LinkName;
                    if (InputImageLinkText == null) return;
                    GetDispImage(InputImageLinkText, true);
                    break;
                case "PositionFix":
                    PositionFixText = obj.LinkName;
                    if (PositionFixText == null) return;
                    GetPositionFix(PositionFixText);
                    break;
                case "MathNumLink":
                    MathNum.Text = obj.LinkName;
                    break;
                default:
                    break;
            }
        }

        public override bool ExeModule()
        {
            try
            {
                Application.Current.Dispatcher.Invoke(() =>
                {
                    mWindowH.Clear();
                    Loaded();
                    ResultTemplateMatchs.Clear();
                });

                if ((null == HPFeaturePatMatchTool) || (null == _HPFeaturePattern) ||
                    (null == DispImage) || (_HPFeaturePattern.ModelData == null))
                {
                    Logger.AddLog(
                        $"流程[{Solution.Ins.GetProjectById(ModuleParam.ProjectID).ProjectInfo.ProcessName}]执行[{ModuleParam.ModuleName}]模块失败，图像模板为空！",
                        eMsgType.Warn
                    );
                    ChangeModuleRunStatus(eRunStatus.NG);
                    return false;
                }
                if (DispImage.IsEmpty)
                {
                    Logger.AddLog(
                        $"流程[{Solution.Ins.GetProjectById(ModuleParam.ProjectID).ProjectInfo.ProcessName}]执行[{ModuleParam.ModuleName}]模块失败，图像源未赋值！",
                        eMsgType.Warn
                    );
                    ChangeModuleRunStatus(eRunStatus.NG);
                    return false;
                }
                HPFeaturePatMatchTool.BasicParam.ShowOutlineStatus = true;
                HPFeaturePatMatchTool.InputImage = DispImage;
                HPFeaturePatMatchTool.Pattern = this._HPFeaturePattern;
                if (IsPositionFix && PositionFixTool != null && mWindowH.ROI != null)
                {
                    PositionFixTool.BasicParam.InitialShape = mWindowH.ROI;
                    PositionFixTool.Run();
                    mWindowH.PositionFixROI = PositionFixTool.Result.CorrectedShape;
                    HPFeaturePatMatchTool.ROI = mWindowH.PositionFixROI;
                }
                else
                {
                    HPFeaturePatMatchTool.ROI = mWindowH.ROI;
                }
                ROI = mWindowH.ROI;
                HPFeaturePatMatchTool.ClearMasks();
                Stopwatch.Restart();
                Param.UpdateParam(HPFeaturePatMatchTool);

                HPFeaturePatMatchTool.Run();
                if (HPFeaturePatMatchTool.Result.MatchInfoList.Count == 0)
                {
                    Logger.AddLog(
                        $"流程[{Solution.Ins.GetProjectById(ModuleParam.ProjectID).ProjectInfo.ProcessName}]执行[{ModuleParam.ModuleName}]模块失败，未匹配到给定模板！",
                        eMsgType.Warn
                    );
                    ChangeModuleRunStatus(eRunStatus.NG);
                    return false;
                }
                int nMatchCnt = 0;
                // 绘制匹配框
                List<CMvdRectangleF> RectangleFList = new List<CMvdRectangleF>();
                foreach (var item in HPFeaturePatMatchTool.Result.MatchInfoList)
                {
                    var matchBox = new CMvdRectangleF(item.MatchBox.CenterX, item.MatchBox.CenterY, item.MatchBox.Width, item.MatchBox.Height);
                    matchBox.Angle = item.MatchBox.Angle;
                    matchBox.BorderColor = new MVD_COLOR(0, 0, 255, 255);
                    RectangleFList.Add(matchBox);
                    nMatchCnt++;
                }
                mWindowH.DrawRectangleFList(RectangleFList);
                // 绘制匹配轮廓
                List<CMvdPointSetF> PointSetFList = new List<CMvdPointSetF>();
                foreach (var item in HPFeaturePatMatchTool.Result.OutlineList)
                {
                    CMvdPointSetF pointSetG = new CMvdPointSetF();
                    CMvdPointSetF pointSetY = new CMvdPointSetF();
                    CMvdPointSetF pointSetR = new CMvdPointSetF();

                    foreach (var point in item.EdgePointList)
                    {
                        if (0 == point.Score)
                        {
                            pointSetG.AddPoint(point.Position.fX, point.Position.fY);
                        }
                        else if (1 == point.Score)
                        {
                            pointSetY.AddPoint(point.Position.fX, point.Position.fY);
                        }
                        else if (2 == point.Score)
                        {
                            pointSetR.AddPoint(point.Position.fX, point.Position.fY);
                        }
                    }

                    pointSetG.BorderColor = new MVD_COLOR(0, 255, 0, 255);
                    pointSetY.BorderColor = new MVD_COLOR(255, 255, 0, 255);
                    pointSetR.BorderColor = new MVD_COLOR(255, 0, 0, 255);

                    if (0 != pointSetG.PointsList.Count)
                    {
                        PointSetFList.Add(pointSetG);
                    }

                    if (0 != pointSetY.PointsList.Count)
                    {
                        PointSetFList.Add(pointSetY);
                    }

                    if (0 != pointSetR.PointsList.Count)
                    {
                        PointSetFList.Add(pointSetR);
                    }
                }
                mWindowH.DrawPointSetFList(PointSetFList);


                string pixelInfo = string.Format("X:{0:F4} Y:{1:F4}", HPFeaturePatMatchTool.Result.MatchInfoList[0].MatchBox.CenterX, HPFeaturePatMatchTool.Result.MatchInfoList[0].MatchBox.CenterY);

                Logger.AddLog(pixelInfo, eMsgType.Success);
                ChangeModuleRunStatus(eRunStatus.OK);
                if (ModuleView != null)
                {
                    CommonMethods.UIAsync(() =>
                    {
                        for (int i = 0; i < HPFeaturePatMatchTool.Result.MatchInfoList.Count; i++)
                        {
                            var result = HPFeaturePatMatchTool.Result.MatchInfoList[i];
                            ResultTemplateMatchs.Add(
                                new ResultTemplateMatch()
                                {
                                    ID = i,
                                    X = result.MatchBox.CenterX,
                                    Y = result.MatchBox.CenterY,
                                    Angle = result.MatchBox.Angle,
                                    Score = result.Score
                                }
                            );
                        }
                    });
                }
                return true;
            }
            catch (Exception ex)
            {

                ChangeModuleRunStatus(eRunStatus.NG);
                Logger.GetExceptionMsg(ex);
                return false;
            }
        }

        public override bool ExeModule(string TemplateName)
        {
            // 查找匹配的模板
            var template = TemplateList.FirstOrDefault(item => item.Name == TemplateName);

            if (template != null)
            {
                // 找到模板后设置并执行模块
                SelectedTemplate = template;
                return ExeModule();
            }
            else
            {
                // 未找到模板，显示错误信息并设置模块状态
                MessageView.Ins.MessageBoxShow($"没有找到模板: {TemplateName}！", eMsgType.Error);
                Logger.AddLog($"流程[{Solution.Ins.GetProjectById(ModuleParam.ProjectID).ProjectInfo.ProcessName}]执行[{ModuleParam.ModuleName}]模块失败，未找到模板！", eMsgType.Warn);
                ChangeModuleRunStatus(eRunStatus.NG);
                return false;
            }
        }


        public override void AddOutputParams()
        {
            base.AddOutputParams();
            if (HPFeaturePatMatchTool != null && HPFeaturePatMatchTool.Result != null && HPFeaturePatMatchTool.Result.MatchInfoList.Count > 0)
            {
                AddOutputParam("匹配个数", "int", HPFeaturePatMatchTool.Result.MatchInfoList.Count);
                AddOutputParam("匹配点", "MatchPoint", HPFeaturePatMatchTool.Result.MatchInfoList[0].MatchPoint);
                AddOutputParam("匹配点X", "MatchPointX", HPFeaturePatMatchTool.Result.MatchInfoList[0].MatchPoint.fX);
                AddOutputParam("匹配点Y", "MatchPointY", HPFeaturePatMatchTool.Result.MatchInfoList[0].MatchPoint.fY);
                AddOutputParam("匹配角度", "MatchAngle", HPFeaturePatMatchTool.Result.MatchInfoList[0].MatchBox.Angle);
                AddOutputParam("匹配X方向尺度", "MatchXScale", HPFeaturePatMatchTool.Result.MatchInfoList[0].ScaleX);
                AddOutputParam("匹配Y方向尺度", "MatchYScale", HPFeaturePatMatchTool.Result.MatchInfoList[0].ScaleY);
                AddOutputParam("匹配框中心X", "float", HPFeaturePatMatchTool.Result.MatchInfoList[0].MatchBox.CenterX);
                AddOutputParam("匹配框中心Y", "float", HPFeaturePatMatchTool.Result.MatchInfoList[0].MatchBox.CenterY);
                AddOutputParam("匹配框角度", "float", HPFeaturePatMatchTool.Result.MatchInfoList[0].MatchBox.Angle);
            }
            else
            {
                AddOutputParam("匹配个数", "int", 0);
                AddOutputParam("匹配点", "MatchPoint", null);
                AddOutputParam("匹配点X", "MatchPointX", null);
                AddOutputParam("匹配点Y", "MatchPointY", null);
                AddOutputParam("匹配角度", "MatchAngle", null);
                AddOutputParam("匹配X方向尺度", "MatchXScale", null);
                AddOutputParam("匹配Y方向尺度", "MatchYScale", null);
                AddOutputParam("匹配框中心X", "float", null);
                AddOutputParam("匹配框中心Y", "float", null);
                AddOutputParam("匹配框角度", "float", null);
            }
        }

        #endregion
    }
}
